package com.zx.erp.market.controller;

import com.github.pagehelper.PageInfo;
import com.zx.erp.annonation.MyLog;
import com.zx.erp.market.bean.Custom;
import com.zx.erp.market.bean.Orderdetails;
import com.zx.erp.market.bean.Orders;
import com.zx.erp.market.service.CustomService;
import com.zx.erp.market.service.OrderdetailsService;
import com.zx.erp.market.service.OrdersService;
import com.zx.erp.repo.service.KcWarehouseBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/market/orderExamine")
@SessionAttributes({"pb"})
public class OrderExamineController {
    @Autowired
    private OrdersService obiz;
    @Autowired
    private OrderdetailsService odbiz;
    @Autowired
    private CustomService cbiz;
    @Autowired
    private KcWarehouseBiz ckwbiz;

    //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 9);
    }


    /**
     * 带条件的分页查询所有
     * @param orders
     * @param datemin  时间
     * @param datemax 时间
     * @param priceMin 金额
     * @param priceMax  金额
     * @param customname 客户名
     * @param pageNum  页码
     * @return
     */
    @RequestMapping("/findPageAll")
    @MyLog("带条件的分页查询所有")
    public String findPageAll(Orders orders, String datemin, String datemax, Double priceMin, Double priceMax, String customname,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model, HttpSession session) {
        List<Integer> cidList = new ArrayList<Integer>();
        Date dateMax = null;
        Date dateMin = null;
        if (customname != null && !customname.equals("")) {
            Custom custom = new Custom();
            custom.setCustomname(customname);
            List<Custom> customList = cbiz.findAll(custom);
            for (Custom c : customList) {
                cidList.add(c.getCreateid());
            }
        }
        if (datemax != null && !datemax.equals("")) {
            dateMax = Date.valueOf(datemax);
        }
        if (datemin != null && !datemin.equals("")) {
            dateMin = Date.valueOf(datemin);
        }
        orders.setStatus(2);
        PageInfo<Orders> pb = obiz.findPageAll(orders, cidList, dateMin, dateMax, priceMin, priceMax, pageNum, 6);
        model.addAttribute("pb", pb);
        model.addAttribute("customname", customname);
        model.addAttribute("orders", orders);
        model.addAttribute("dateMin", dateMin);
        model.addAttribute("dateMax", dateMax);
        model.addAttribute("priceMin", priceMin);
        model.addAttribute("priceMax", priceMax);

        return "forward:/market/orderExamine/orderExamineList.html";
    }


    /**
     * 跳转到订单审核页面
     * @param orderid
     * @return
     */
    @RequestMapping("/goAuditOrder")
    public String goAuditOrder(String orderid, Model model) {
        Orders order = obiz.findBy(orderid);
        List<Orderdetails> orderDs = odbiz.findByOrderId(orderid);
        model.addAttribute("order", order);
        model.addAttribute("orderDs", orderDs);
        return "forward:/market/orderExamine/orderExamine.html";
    }

    /**
     * 审核
     * @param orders
     * @return
     */
    @RequestMapping("/auditOrder")
    @MyLog("审核")
    public String auditOrder(Orders orders,Model model) {
        Boolean flag = false;
        flag = obiz.editOrders(orders);
        PageInfo<Orders> pb = (PageInfo<Orders>) model.getAttribute("pb");
        Integer pageNum=pb.getPageNum();
        if (flag) {
            return "redirect:/market/orderExamine/findPageAll?pageNum="+pageNum;
        } else {
            return "";
        }
    }

}
